package org.michael.u.brg.by.spring.aop.abcthreads;

import java.util.concurrent.ExecutorService;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Spawned by the Runner B (the "guide"), notifies the Runner B of completion by calling
 * {@link RunnerB#releaseC()}. 
 */
public class RunnerC implements Runnable, ApplicationContextAware {

	private long startupMillis;
	private ApplicationContext springAppcontext;
	private ExecutorService exec;
	
	private RunnerB guide;

	private Long id;
	private long delay; 

	public void run() {

		System.out.printf("%n      C-%d: start at %,d", id, System.currentTimeMillis() - startupMillis);
		try { 
			Thread.sleep(delay); 
		} 
		catch(InterruptedException x) {
			x.printStackTrace();
		}
		guide.releaseC();
		System.out.printf("%n      C-%d: end at %,d", id, System.currentTimeMillis() - startupMillis);
	}

	public void setIdGetter(IdProvider id) {
		this.id = id.getNext();
	}

	public void setDelay(Long delay) {
		this.delay = delay;
	}

	public void setApplicationContext(ApplicationContext value)
			throws BeansException {
		springAppcontext = value;
		
	}

	public void setExec(ExecutorService svc) {
		this.exec = svc;
	}
	
	public Long getId() {
		return id;
	}

	public RunnerB getGuide() {
		return guide;
	}

	public void setGuide(RunnerB guide) {
		this.guide = guide;
	}

	public void setStartupMillis(long startupMillis) {
		this.startupMillis = startupMillis;
	}
}
